﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SharpDNDTools.UserControls.Common
{
    public partial class ucMainViewer : UserControl
    {
        //vars for custom column/row drag/drop operations

        #region Delegates

        public delegate void AddButtonClickedHandler(object sender, EventArgs e);

        public delegate void GridDoubleClickedHandler(object sender, EventArgs e);

        public delegate void ItemSortedHandler(object sender, EventArgs e);

        #endregion

        private readonly BindingSource _dataSource;
        private int DragDropCurrentIndex = -1;

        private int DragDropSourceIndex;
        private int DragDropTargetIndex;
        private int DragDropType; //0=column, 1=row

        public ucMainViewer(BindingSource dataSource)
        {
            InitializeComponent();

            _dataSource = dataSource;

            dgvBase.DataSource = _dataSource;

            dgvBase.AutoGenerateColumns = false;
            dgvBase.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvBase.AllowUserToOrderColumns = false;
            dgvBase.AllowUserToAddRows = false;

            if (dgvBase != null)
            {
                dgvBase.Columns["RESOURCELOCATION"].Visible = false;
                dgvBase.Columns["RESOURCEKEY"].Visible = false;
                dgvBase.Columns["RESOURCETYPE"].Visible = false;
                dgvBase.Columns["ResourceID"].Visible = false;
                dgvBase.Columns["ResourceSortID"].Visible = false;

                foreach (DataGridViewColumn dgvc in dgvBase.Columns)
                {
                    dgvc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dgvc.ReadOnly = true;
                }
            }
        }

        public event AddButtonClickedHandler OnAddButtonClicked;

        public event GridDoubleClickedHandler OnGridDoubleClicked;

        public event ItemSortedHandler OnItemSorted;

        private void tbMain_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            if (e.Button == tbbAddNew && OnAddButtonClicked != null)
            {
                OnAddButtonClicked(this, new EventArgs());
            }
        }

        private void dgvBase_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgvBase_DragDrop(object sender, DragEventArgs e)
        {
            //runs after a drag/drop operation for column/row has completed
            if (this.AllowDrop)
            {
                if (e.Effect == DragDropEffects.Move)
                {
                    Point ClientPoint = this.PointToClient(new Point(e.X, e.Y));
                    if (DragDropType == 1)
                    {
                        //if this is a row drag/drop operation
                        DragDropTargetIndex = dgvBase.HitTest(ClientPoint.X, ClientPoint.Y).RowIndex;
                        if (DragDropTargetIndex > -1 && DragDropCurrentIndex < dgvBase.RowCount - 1)
                        {
                            DragDropCurrentIndex = -1;
                            DataGridViewRow SourceRow = e.Data.GetData(typeof (DataGridViewRow)) as DataGridViewRow;
                            dgvBase.Rows.RemoveAt(DragDropSourceIndex);
                            dgvBase.Rows.Insert(DragDropTargetIndex, SourceRow);
                            dgvBase.Rows[DragDropTargetIndex].Selected = true;
                            dgvBase.CurrentCell = dgvBase[0, DragDropTargetIndex];

                            object boundObject = _dataSource[DragDropSourceIndex];
                            _dataSource.RemoveAt(DragDropSourceIndex);
                            _dataSource.Insert(DragDropTargetIndex, boundObject);

                            if (OnItemSorted != null)
                            {
                                OnItemSorted(_dataSource, new EventArgs());
                            }
                        } //end if
                    } //end if
                } //end if
            } //end if
            base.OnDragDrop(e);
        }

        private void dgvBase_DragOver(object sender, DragEventArgs e)
        {
            //runs while the drag/drop is in progress
            if (this.AllowDrop)
            {
                e.Effect = DragDropEffects.Move;
                if (DragDropType == 1)
                {
                    //row drag/drop
                    int CurRow = dgvBase.HitTest(this.PointToClient(new Point(e.X, e.Y)).X, this.PointToClient(new Point(e.X, e.Y)).Y).RowIndex;
                    if (DragDropCurrentIndex != CurRow)
                    {
                        DragDropCurrentIndex = CurRow;
                        this.Invalidate(); //repaint
                    } //end if
                } //end if
            } //end if
            base.OnDragOver(e);
        }

        private void dgvBase_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgvBase.RowCount > e.RowIndex && e.RowIndex >= 0 && OnGridDoubleClicked != null)
            {
                OnGridDoubleClicked(dgvBase.Rows[e.RowIndex].Cells["ResourceID"].Value, new EventArgs());
            }
        }
    }
}